package com.hlc.helloWord.common.aspect;

import com.hlc.helloWord.common.token.TokenUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by helinchong on 2017/12/26.
 */
@Aspect
public class SignInAspect {

    @Autowired
    TokenUtil tokenUtil;

    @Before("execution(* com.hlc.helloWord..controller..*.*(..))")
    public void befor(JoinPoint joinPoint){
        //此处做token验证 先过滤掉不需要用验证的界面如登录页面
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        //有多个路径都不需要验证的话配个属性文件什么的 数据维护都行 怎么方便怎么来
        if(request.getRequestURL().toString().contains("user/signIn")){
           return;
        }
        tokenUtil.checkToken(request.getHeader("Authentication"));
    }
}
